package com.wxgo.controller.front;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.wxgo.common.model.Industry;
import com.wxgo.common.model.News;
import com.wxgo.common.model.Outtype;
import com.wxgo.common.model.Province;
import com.wxgo.consts.NewsConsts;
import com.wxgo.dao.NewsDao;
import com.wxgo.dao.PubDao;
import com.wxgo.factory.DaoFactory;

public class FrontController extends Controller {

	public void index() {
		List<Industry> industryList = DaoFactory.industryDao()
				.find("select title,id,indate from industry where type=0 limit 0,3");
		setAttr("industryList", industryList);
		render("/front/index.html");
	}

	public void cases() {
		render("/front/case.html");
	}

	public void industry() {
		List<Industry> industryList = DaoFactory.industryDao()
				.find("select title,id,indate from industry where type=0 limit 0,18");
		setAttr("industryList", industryList);
		render("/front/industry.html");
	}

	public void industryDetail() {
		Integer id = getParaToInt("id", 1);
		Industry aboutus = DaoFactory.industryDao().findById(id);
		setAttr("article", aboutus);
		render("/front/industryDetail.html");
	}

	public void contactus() {
		Industry article = DaoFactory.industryDao().findByType(NewsConsts.CONCACTUS);
		setAttr("article", article);
		render("/front/contactus.html");
	}

	public void joinin() {
		Industry article = DaoFactory.industryDao().findByType(NewsConsts.JOINUS);
		setAttr("article", article);
		render("/front/joinin.html");
	}

	public void aboutus() {
		Industry aboutus = DaoFactory.industryDao().findByType(NewsConsts.ABOUTUS);
		setAttr("article", aboutus);
		render("/front/aboutus.html");
	}

	public void service() {
		Industry aboutus = DaoFactory.industryDao().findByType(NewsConsts.SERVICE);
		setAttr("article", aboutus);
		render("/front/aboutus.html");
	}

	public void company() {
		Industry aboutus = DaoFactory.industryDao().findByType(NewsConsts.COMPANY);
		setAttr("article", aboutus);
		render("/front/aboutus.html");
	}

	private NewsDao newsDao = Enhancer.enhance(NewsDao.class);
	private PubDao pubDao = Enhancer.enhance(PubDao.class);

	public void news() {
		List<Outtype> outtypeList = pubDao.findOuttype();
		setAttr("outtypeList", outtypeList);
		List<Province> provinceList = pubDao.findProvince();
		setAttr("provinceList", provinceList);
		render("/front/newslist.html");
	}

	public void newslist() {
		Integer limit = getParaToInt("limit", 10);
		Integer page = getParaToInt("page", 1);
		String newsnameOrUrl = getPara("newsname", "");
		String province = getPara("province", "");
		String outtype = getPara("outtype", "");
		Long max = getParaToLong("max", 0L);
		Long min = getParaToLong("min", 0L);
		Integer other = getParaToInt("other", 0);
		Integer intype = getParaToInt("intype", 2);

		String where = " where 1=1 ";
		List<String> objArr = new ArrayList<String>();
		if (newsnameOrUrl.length() > 0) {
			try {
				newsnameOrUrl = URLDecoder.decode(URLDecoder.decode(newsnameOrUrl, "UTF-8"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				newsnameOrUrl = "";
			}
			where += " and (newsname like ? or site like ?)";
			objArr.add("%" + newsnameOrUrl + "%");
			objArr.add("%" + newsnameOrUrl + "%");
		}
		if (province.length() > 0) {
			province += "999";
			where += " and pcode in(" + province + ")";
		}

		if (max > min && min >= 0) {
			if (min == 0) {
				where += " and vipprice <= ?";
				objArr.add(max.toString());
			} else {
				where += " and vipprice >= ? and vipprice <=?";
				objArr.add(min.toString());
				objArr.add(max.toString());
			}
		}

		if (outtype.length() > 0) {
			outtype += "0";
			where += " and outtype in (" + outtype + ")";
		}

		if (other > 0) {
			if (other == 1) {
				where += " and remark like '%周末%'";
			} else if (other == 2) {
				where += " and remark like '%微商%'";
			} else if (other == 3) {
				where += " and remark like '%医疗%'";
			} else if (other == 4) {
				where += " and remark like '%加盟%'";
			}
		}
		if (intype != 2) {
			where += " and intype = " + intype;
		}

		long count = newsDao.count(where, objArr.toArray());
		where += " limit " + (page - 1) * limit + "," + limit;
		List<News> newsList = newsDao.find(where, objArr.toArray());
		renderJson(new Ret().set("code", 0).set("msg", "").set("count", count).set("data", newsList));
	}
}
